#include"roomshow.h"
#include "ui_roomshow.h"
#include"lib/sql.h"
#include"dlg_roomcontrol.h"
#include <QDialog>

roomshow::roomshow(QWidget *parent,int f)
    : QDialog(parent)
    , ui(new Ui::roomshow)
{
    flag = f;

    ui->setupUi(this);
    this->page2=new dlg_roomcontrol;
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setModel(&rs_mode);
}

roomshow::~roomshow()
{
    delete ui;
}

//查看数据库信息
void roomshow::initPage(QString str)
{
    if(flag == 1){              //若是用户则隐藏部分功能
        ui->btn_add_alt->hide();
        ui->btn_del->hide();
        ui->label_2->hide();
    }

    auto l = Sql::getinstance()->getRoom(str);//拿数据显示到页面
    rs_mode.clear();
    rs_mode.setHorizontalHeaderLabels(QStringList{"门牌号","状态","容纳人数","开始时间","结束时间","投影仪"});
    for(int i=0;i<l.size();i++){
        QList<QStandardItem*> item;
        for(int j=0;j<l[i].size();j++){
            item.append(new QStandardItem(l[i][j]));
        }
        rs_mode.appendRow(item);
    }
};
//搜索框显示
void roomshow::on_lineEdit_textChanged(const QString &arg1)
{
    QString strSql = QString("where id like '%%1%'").arg(arg1);
    keyword=strSql;
}


//查询按钮
void roomshow::on_btn_find_clicked()
{
    initPage(keyword);
}

//删除会议室
void roomshow::on_btn_del_clicked()
{
    int r = ui->tableView->currentIndex().row();
    if(r < 0){
        QMessageBox::information(nullptr,"信息","无选择会议室");
    }
    else{
        auto id = rs_mode.item(r,0)->text();
        Sql::getinstance()->delRoom(id);
        initPage();
        QMessageBox::information(nullptr,"信息","删除成功");
    }
}

//增加会议室
//修改会议室信息
void roomshow::on_btn_add_alt_clicked()
{
    this->page2->show();
    initPage();
}

//预约按钮
void roomshow::on_btn_resv_clicked()
{

    int r = ui->tableView->currentIndex().row();
    if(r < 0){
        QMessageBox::information(nullptr,"信息","无选择会议室");
    }
    else{
        auto id = rs_mode.item(r,0)->text();
        Sql::getinstance()->addres(id);
        initPage();
        QMessageBox::information(nullptr,"信息","预约成功");
    }
}
